package loginview;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import static users.LoginFun.*;

public class Register extends JFrame implements MouseListener {
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JPanel p4=new JPanel();
    JPanel p5=new JPanel();
    JPanel p6=new JPanel();

    JLabel welcome=new JLabel("注册页面");
    JLabel username=new JLabel("账号:     ");
    JLabel password1=new JLabel("密码:     ");
    JLabel password2=new JLabel("确认密码: ");
    JLabel codeText=new JLabel("   验证码:    ");

    JTextField usernameText=new JTextField(11);
    JPasswordField passwordField1=new JPasswordField(11);
    JPasswordField passwordField2=new JPasswordField(10);

    //添加验证码输入框
    JTextField codeInput=new JTextField(7);
    //生成随机验证码
    String codeStr= CodeUnit.getCode();
    JLabel rightCode=new JLabel(codeStr);

    JButton register=new JButton("立即注册");
    JButton Return=new JButton("返回登录界面");

    public Register(){
        //初始化界面
        initJFrame();
        //在这个界面添加内容
        initView();
        //让页面展示出来
        this.setVisible(true);

    }

    private void initJFrame() {
        this.setTitle("注册");
        this.setSize(500, 400);
        this.setLayout(new GridLayout(7, 1,10,10));
        //设置页面置顶
        //this.setAlwaysOnTop(true);
        //设置页面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void initView() {
        welcome.setFont(new Font("微软雅黑", Font.BOLD, 20));
        Font size=new Font("微软雅黑", Font.PLAIN, 15);
        username.setFont(size);
        password1.setFont(size);
        password2.setFont(size);
        codeText.setFont(size);
        rightCode.setFont(new Font("微软雅黑", Font.BOLD, 13));

        p1.add(welcome);
        p2.add(username);
        p2.add(usernameText);
        p3.add(password1);
        p3.add(passwordField1);
        p4.add(password2);
        p4.add(passwordField2);
        p5.add(codeText);
        p5.add(codeInput);
        p5.add(rightCode);
        p6.add(register);
        p6.add(Return);

        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        this.add(p5);
        this.add(p6);

        //验证码鼠标监听
        rightCode.addMouseListener(this);

        //立即注册按钮监听
        register.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object obj=e.getSource();
                if(obj==register){
                    //获取用户输入的验证码、用户名和密码
                    String code=codeInput.getText();
                    String username=usernameText.getText();
                    char[] passwordChars1 = passwordField1.getPassword();
                    String password1=new String(passwordChars1);
                    char[] passwordChars2 = passwordField2.getPassword();
                    String password2 =new String(passwordChars2);
                    //System.out.println(username+","+password);

                    //验证码判断
                    if(code.isEmpty()){
                        showJDialog("验证码为空");
                        refreshCode();
                        return;
                    }

                    if(!(code.equalsIgnoreCase(codeStr))){
                        showJDialog("验证码错误");
                        refreshCode();
                        return;
                    }

                    //判断用户名或密码输入是否为空
                    if(username.isEmpty()|| password1.isEmpty()||password2.isEmpty()){
                        showJDialog("用户名或密码为空，请重新输入");
                        //refreshCode();
                        //清空用户名输入框和密码输入框
                        //usernameInput.setText("");
                        //passwordInput.setText("");
                        return;
                    }

                    //判断两次密码是否一致
                    if(!(password1.equals(password2))){
                        showJDialog("两次输入的密码不一致！");
                        //refreshCode();
                        //清空用户名输入框和密码输入框
                        //usernameInput.setText("");
                        //passwordInput.setText("");
                    }
                    try {
                        boolean flag=userSearch(username);
                        if(flag){
                            showJDialog("用户已存在！");
                            refreshCode();
                        }else{
                            Register.this.setVisible(false);
                            new AddMessage(username,password1);
                        }
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }

                }
            }
        });

        //返回登录按钮监听
        Return.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Register.this.setVisible(false);
                new Login();
            }
        });
    }

    private void refreshCode() {
        // 生成新的随机验证码
        String newCode = CodeUnit.getCode();

        // 更新验证码标签显示
        codeStr=newCode;
        rightCode.setText(newCode);
        rightCode.repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj=e.getSource();
        if(obj==rightCode){
            refreshCode();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
